package com.training.persistence.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.training.persistence.entity.BowlGame;

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
@Repository(value="bowlGameDao")
public class BowlGameDaoImpl implements BowlGameDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public BowlGame saveBowlGame(BowlGame bowlgame) {
		this.getSession().save(bowlgame);
		return bowlgame;
	}

	public BowlGame updateBowlGame(BowlGame bowlgame) {
		this.getSession().saveOrUpdate(bowlgame);
		return bowlgame;
	}

	public List<BowlGame> findAllBowlGames() {
		return this.getSession().createQuery("from BOWLGAME").list();
	}

	public BowlGame findBowlGameById(Long id) {
		return null;
	}

	public BowlGame findBowlGameByBowlGameId(Integer bowlgameId) {
		List<BowlGame> bowlgames = this.getSession()
				.createQuery("from BowlGame p where p.bowlgameId = :bowlgameId")
				.setInteger("bowlgameId", bowlgameId).list();
		if (bowlgames.isEmpty()) {
			return null;
		} else {
			return bowlgames.get(0);
		}
	}
    
    public Session getSession(){
    	return sessionFactory.getCurrentSession();
    }
    
    
}
